package com.penghaisoft.microwms.common.util;

import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.FromToObj;

/**
 * @Title: CommonUtils.java
 * @Description: 存放一些公共方法
 * @author zxcq0
 * @date 2017/2/28 14:21
 * @version V1.0
 */

public class CommonUtils {

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
			"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
			"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" };

	/**
	 * @Title: generateShortUuid
	 * @Description: 短8位UUID
	 * @author zhangxu
	 * @2017年9月21日:2017年9月21日:上午10:27:02
	 * @param @return
	 * @return String
	 * @version V0.1
	 * @throws
	 */
	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}

	/**
	 * @Description: 获取一个UUID，去掉-
	 * @author zxcq0
	 * @date 2017/2/28 14:23
	 * @param
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * @Description: 获取一组uuid
	 * @author zxcq0
	 * @date 2017/2/28 14:25
	 * @param
	 * @return
	 */
	public static List<String> getUUIDs(int size) {
		List<String> uuids = new ArrayList<String>();
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				uuids.add(getUUID());
			}
		}
		return uuids;
	}

	public static FromToObj getFromTo(String str, int formatType)
			throws ParseException {
		// TODO 后期改为年月日时分秒
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String prefix = str.substring(0, 2);
		String data = str.substring(3, str.length());
		FromToObj fromToObj = new FromToObj();
		if (formatType == Constant.FROM_TO_DATE) {
			if (prefix.equals("bt")) {
				Date dateFrom = sdf.parse(data.split(",")[0]);
				Date dateTo = sdf.parse(data.split(",")[1]);
				fromToObj.setFromDate(dateFrom);
				fromToObj.setToDate(dateTo);
			} else {
				Date date = sdf.parse(data);
				if (prefix.equals("gt")) {
					fromToObj.setFromDate(date);
				} else if (prefix.equals("lt")) {
					fromToObj.setToDate(date);
				} else if (prefix.equals("eq")) {
					fromToObj.setDate(date);
				}
			}
		} else if (formatType == Constant.FROM_TO_NUM) {
			if (prefix.equals("bt")) {
				Double minNum = Double.valueOf(data.split(",")[0]);
				Double maxNum = Double.valueOf(data.split(",")[1]);
				fromToObj.setMinNum(minNum);
				fromToObj.setMaxNum(maxNum);
			} else {
				Double num = Double.valueOf(data);
				if (prefix.equals("gt")) {
					fromToObj.setMinNum(num);
				} else if (prefix.equals("lt")) {
					fromToObj.setMaxNum(num);
				} else if (prefix.equals("eq")) {
					fromToObj.setNum(num);
				}
			}
		} else {
			fromToObj = null;
		}
		return fromToObj;
	}

	/**
	 * 产生MD5的hash
	 * 
	 * @param str
	 * @return
	 */
	public static String md5Hash(String str) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		md.update(str.getBytes());
		byte byteData[] = md.digest();

		// convert the byte to hex format method 1
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		}

		// convert the byte to hex format method 2
		// StringBuffer hexString = new StringBuffer();
		// for (int i=0;i<byteData.length;i++) {
		// String hex=Integer.toHexString(0xff & byteData[i]);
		// if(hex.length()==1) hexString.append('0');
		// hexString.append(hex);
		// }
		return sb.toString();
	}

	public static boolean isBlank(String str) {
		if (str == null || str.length() == 0) {

			return true;
		}
		return false;
	}
	
	/** 
	 * @Title reflectObj
	 * @Description 利用反射，将对象的值赋给另外一个具有相同属性的对象
	 * @author luoteng
	 * @date 2017年6月14日:下午3:02:35
	 * @param obFrom 用于赋值的对象
	 * @param obTo 待赋值的对象
	 * @throws Exception void
	 */
	public static void reflectObj(Object obFrom, Object obTo) throws Exception{
		Class clsFrom = Class.forName(obFrom.getClass().getName());
		Class clsTo = Class.forName(obTo.getClass().getName());
		
		Field[] fieldsFrom = clsFrom.getDeclaredFields();
		Field[] fieldsTo = clsTo.getDeclaredFields();
		
		for(Field fieldFrom : fieldsFrom){
			String fieldNameFrom = fieldFrom.getName();
			fieldFrom.setAccessible(true);
			Object val = fieldFrom.get(obFrom);
			
			for(Field fieldTo : fieldsTo){
				fieldTo.setAccessible(true);
				if(fieldTo.getName().equals(fieldNameFrom)){
					fieldTo.set(obTo, val);
				}
			}
		}
	}
}
